<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
</head>

<body>
    <!-- 2.准备容器 -->
    <div id="app">
        <!-- key属性，配合v-for使用 -->
        <!-- v-for="(item,index) in 数组" -->
        小黑书架
        <ul>
            <!-- <li v-for="item in bookList" :key="item.id"> -->
            <li v-for="item in bookList" v-bind:key="item.id">
                {{item.name}} {{item.author}}
                <button @click="deleteBooks(item.id)">删除</button>
            </li>
        </ul>
    </div>

    <!-- 1。导入vue.js -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>

    <script>
        <!-- 3. 创建Vue实例对象 -->
        const app = new Vue({
            // < !--容器 -->
            el: "#app",
            data: {
                bookList: [
                    { id: 1, name: '《红楼梦》', author: '曹雪芹' },
                    { id: 2, name: '《西游记》', author: '吴承恩' },
                    { id: 3, name: '《水浒传》', author: '施耐庵' },
                    { id: 4, name: '《三国演义》', author: '罗贯中' }
                ]
            },
            methods: {
                deleteBooks(id) {
                    // 数组的filter方法
                    // filter不影响数组本身，需要赋值
                    this.bookList = this.bookList.filter(v => v.id !== id)
                }
            },
        });
    </script>
</body>

</html>